//
//  NewsViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/2.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsTableViewCell.h"
#import "NewsEmptyView.h"
#import "GetMessageListAPI.h"
#import "NSDate+FormatterString.h"
#import "MJRefresh.h"

@interface NewsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NewsEmptyView *emptyView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,assign)int page;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)refreshBaseControlValue
{
    self.page = 1;
    [self getMessageListWithPage:self.page];
}

#pragma mark -- 事件处理
- (void)getMessageListWithPage:(int)page
{
    GetMessageListAPI *api = [[GetMessageListAPI alloc] initWithDic:@{@"page":@(page).stringValue}];
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        NSDictionary *dict = [request responseJSONObject];
        NSArray *array = dict[@"data"];
        if (self.page == 1)
        {
            if (array.count > 0)
            {
                [self.dataSource removeAllObjects];
                [self.dataSource addObjectsFromArray:array];
                self.emptyView.hidden = YES;
            }
            else
            {
                self.emptyView.hidden = NO;
            }
        }
        else
        {
            if (array.count > 0)
            {
                [self.dataSource addObjectsFromArray:array];
            }
            else
            {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView reloadData];
    } failure:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showErrorWithStatus:request.responseJSONObject[@"errmsg"]];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(340);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsTableViewCell *cell = [NewsTableViewCell cellWithTableView:tableView forIndexPath:indexPath];
    NSDictionary *dict = self.dataSource[indexPath.row];
    cell.dateLabel.text = [NSDate getDateWithStringSince1970ToSecond:dict[@"ctime"] stringDateFormatter:@"YYYY/MM/dd HH:mm"];
    cell.contentLabel.text = dict[@"content"];
    if ([dict[@"title"] isEqualToString:@"报警"])
    {
        cell.typeButton.selected = YES;
    }
    else
    {
        cell.typeButton.selected = NO;
    }


    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self getMessageListWithPage:self.page];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page ++;
            [self getMessageListWithPage:self.page];
        }];
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

- (NewsEmptyView *)emptyView
{
    if(!_emptyView)
    {
        UIView *superView = self.view;
        _emptyView = [[NewsEmptyView alloc]init];
        [superView addSubview:_emptyView];
        _emptyView.backgroundColor = rgba(6,27,56,1);
    }
    return _emptyView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}



- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(250));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.tableView);
    }];
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"消息"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
